import shutil
import sys

import joblib
from matplotlib import pyplot as plt
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler

from orbitP.script import config
from datetime import datetime, timedelta
from orbitP.script.timefeatures import time_features
from orbitP.script.plot import plot_origin_error, plot_error, plot_SULT, plot_RSW, plot_connetError
from orbitP.script.util import get_SULT, splitDataset, get_3DError
import numpy as np
import os
from tqdm import tqdm
import pandas as pd
import torch


def get_file_paths(directory):
    file_paths = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            file_path = os.path.join(root, file)
            file_paths.append(os.path.normpath(file_path))
    file_paths = sorted(file_paths)
    return file_paths

def clean_showErrorDir():
    if os.path.exists(config.showErrorDir):
        shutil.rmtree(config.showErrorDir)

    if not os.path.exists(config.showErrorDir):
        os.mkdir(config.showErrorDir)

def showOriginError():
    df_obserData = pd.read_csv(config.dataSetDir + 'df_obsData.csv')
    df_prdData = pd.read_csv(config.dataSetDir + 'df_prdData.csv')
    df_stampObs = pd.read_csv(config.dataSetDir + 'df_stampObs.csv')
    df_stampPrd = pd.read_csv(config.dataSetDir + 'df_stampPrd.csv')
    obsData = df_obserData.to_numpy(dtype=np.float32)
    prdData = df_prdData.to_numpy(dtype=np.float32)
    stampObs = df_stampObs.to_numpy(dtype=np.float32)
    stampPrd = df_stampPrd.to_numpy(dtype=np.float32)

    obsData, prdData, stampObs, stampPrd = splitDataset(obsData, prdData, stampObs, stampPrd, runType='train', sca=False)
    obsData_train = obsData[0];obsData_val = obsData[1];obsData_test = obsData[2]
    prdData_train = prdData[0];prdData_val = prdData[1];prdData_test = prdData[2]
    stampObs_train = stampObs[0];stampObs_val = stampObs[1];stampObs_test = stampObs[2]
    stampPrd_train = stampPrd[0];stampPrd_val = stampPrd[1];stampPrd_test = stampPrd[2]
    prdData_test = prdData_test[:,:config.axis]
    test_size = prdData_test.shape[0] // config.predicting_length
    prdData_test = prdData_test.reshape([test_size,config.predicting_length,config.axis])
    zeros = np.zeros(prdData_test.shape)
    get_3DError(prdData_test[:,:,:config.axis],zeros)

if __name__ == "__main__":
    showOriginError()
